<div  class="shadowBox">
<div class="height40 TextMiddle40 card_item_toolbar">
<a href="#" onclick="javascript:fileListJs.addFile()" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add_file'">上传文件</a>
<a href="#" onclick="javascript:fileListJs.addFolder()" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add_folder'">添加文件夹</a>
</div>
</div>
<div id="fileListLeft" style="padding-left:10px;">
<ul id="fileList"></ul>
</div>


<div id="folderMenu" class="easyui-menu" data-options="onClick:fileListJs.folderMenuHandler" style="width:120px;">
	<div data-options="name:'view'">查看详情</div>
	<div data-options="name:'auth'">管理权限</div>

	<div data-options="name:'update'">更新</div>
	<div class="menu-sep"></div>
	<div data-options="name:'delete'">删除</div>
</div>
<div id="fileMenu" class="easyui-menu" data-options="onClick:fileListJs.fileMenuHandler" style="width:120px;">
	<div data-options="name:'view'">查看详情</div>
	<div data-options="name:'download'">下载</div>
	<div data-options="name:'update'">更新</div>
	<div class="menu-sep"></div>
	<div data-options="name:'delete'">删除</div>
</div>

<div id="fileDiag" class="easyui-dialog"  closed="true" style="width:600px;height:300px;padding:10px;">
</div>

<script type="text/javascript">
(function(fileListJs, $, undefined ) {
	fileListJs.init = function()
	{
		$('#fileList').tree({
		    url: 'dispatch/utility/file/list',
		    animate:true,
		    lines: true,
		    loadFilter: function(data){
		    	
		    	var res = data.result;
		        if(res == "SUCCESS")
		        {
		        	return jQuery.parseJSON(data.payload.value);
		        }else if(res == "COMPONENT_ERROR")
		        {
		        	$.messager.alert("Error",data.payload.value);
		        }else
		        {
		        	$.messager.alert("Error",data.result);
		        }
		        
		    	
		    },
		    onBeforeLoad : function(node,param){
		    	
		    	if(node == null)return true;
		    	if(node.children == null)
		    	{
		    		return true;
		    	}
		    	else
		    		{
		    		return false;
		    		}
		    },
		    onContextMenu: function(e, node){
		    	
		    	e.preventDefault();
		    	
				// select the node
				$('#fileList').tree('select', node.target);
				if(node.folder)
				{
					// display context menu
					$('#folderMenu').menu('show', {
						left: e.pageX,
						top: e.pageY
					});
				}
				else
				{
					// display context menu
					$('#fileMenu').menu('show', {
						left: e.pageX,
						top: e.pageY
					});
				}


		    }
		});

	}
	
	function opendiag(uri,title){
		$('#fileDiag').dialog("open").dialog('setTitle',title);
		$('#fileDiag').dialog('refresh',uri);
		
	};
	fileListJs.folderMenuHandler = function(item){
		var node = $('#fileList').tree('getSelected');
		if(item.name == 'view')
		{
			view(node.id,'folder');
		}else if(item.name == 'auth')
		{
			workbenchJs.open1('component/utility/ResourceAuth.jsp?id='+node.id,this);
		}else
			if(item.name == 'delete')
			{
				loadData("dispatch/utility/folder/delete?id="+node.id,deleteResult,node);
			}
			else if(item.name == 'update')
			{
				fileActionJs.setId(node.id);
				fileActionJs.setAddActionCallBack(function(result){
					$('#fileDiag').dialog('close');
				});
				opendiag('component/utility/updateFolder.html','修改文件夹');
			}
	}
	fileListJs.fileMenuHandler = function(item){
		var node = $('#fileList').tree('getSelected');
		if(item.name == 'view')
		{
			view(node.id,'file');
		}else
		if(item.name == 'download')
		{
			download(node.id);
		}else
		if(item.name == 'delete')
		{
			loadData("dispatch/utility/file/delete?id="+node.id,deleteResult,node);
		}
	}
	
	function loadData(url,func,para){
		$.ajax({
            type: "post",
            url: url,
            dataType:"json",
            success: function (result) {
            	func(result,para);
            }
            
        });
	}
	function deleteResult(result,node){
    	var res = result.result;
        if(res == "SUCCESS")
        {
        	$('#fileList').tree('remove',node.target);
        }else if(result.result == "NOT_AUTHORIZED")
        {
        	$.messager.alert("Error","您没有权限 " + action);
        }else if(res == "COMPONENT_ERROR")
        {
        	$.messager.alert("Error",result.payload.value);
        }else
        {
        	$.messager.alert("Error",result.result);
        }
	}
	function download(id){
		 var form = $("<form>");
	       form.attr('style','display:none');
	       form.attr('target','');
	       form.attr('method','get');
	       form.attr('action',"file/down");
	      
	       var input1 = $('<input>'); 
	       input1.attr('type','hidden'); 
	       input1.attr('name','id'); 
	       input1.attr('value',id); 
	      
	       $('body').append(form);
	       form.append(input1);
	       form.submit().remove();
	}
	
	function view(id,type){
		opendiag('component/utility/viewResource.jsp?id='+id+'&type='+type,'资源详细信息');
	}
	
	fileListJs.addFile = function(){
		workbenchJs.open1('component/utility/addFile.html',this);
	}
	fileListJs.addFolder = function(){
		workbenchJs.open1('component/utility/addFolder.html',this);
	}
}( window.fileListJs = window.fileListJs || {}, jQuery ));

    $(function(){
    	fileListJs.init();
    });
</script>
 <style type="text/css">
 	#fileListLeft{
 		float:left;
 		height:auto;
 	}
 	#fileListRight{
 		float:left;
 		height:auto;
 		padding-left:10px;
 		width:75%;
 	}
 	#fileListTop{
 		height:auto;
 		margin:0;
 		padding:0;
 	}
 </style>